﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using Time.Foramework;

namespace Time.DB
{

    public class OleDBHelp
    {
        private static OleDbConnection myCon = null;
        //private static string Address = System.Environment.CurrentDirectory + @"D:\程序备份\中航数据采集D.MDB";
        //配置文件地址
       static  string  configPath = Application.StartupPath + "\\Config.ini";

        private static string Address = ConfigOperate.ReadIniKeys("DBAddress", "zhAddress", configPath);// @"D:\程序备份\中航数据采集D.MDB";
        public static string objConStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Address + ";Persist Security Info=False"; //ConfigurationManager.AppSettings["conStr"];

        public OleDBHelp()
        {
            try
            {
                myCon = new OleDbConnection(objConStr);
                open();
            }
            catch (Exception e)
            {
                throw new Exception("数据连接异常，请检查文件是否有异常。");
            }
            finally
            {
                close();
            }
        }

        public OleDBHelp(string strcon)
        {
            try
            {
                objConStr = strcon;
                myCon = new OleDbConnection(strcon);
                open();
            }
            catch (Exception e)
            {
                throw new Exception("数据连接异常，请检查文件是否有异常。");
            }
            finally
            {
                close();
            }
        }
        public Boolean ExcOperationData(string strSql,OleDbParameter[] tempOleDbParameter)
        {
            Boolean bl = false;
            myCon = new OleDbConnection(objConStr);
            OleDbCommand cmd = new OleDbCommand(strSql, myCon);
            try
            {
                myCon.Open();
                if (tempOleDbParameter != null)
                {
                    foreach (OleDbParameter parameter in tempOleDbParameter)
                    {
                        cmd.Parameters.Add(parameter);
                    }
                }
                cmd.ExecuteNonQuery();
                bl = true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                myCon.Close();
            }
            return bl;

        }
        public DataTable getDataTable(string tableName, string Fields = " * ", string fWhere = null)
        {
            string sqlStr = string.Format("select {0} from {1} ", Fields, tableName);
            if (!string.IsNullOrEmpty(fWhere))
            {
                sqlStr += " where " + fWhere ;
            }
            myCon = new OleDbConnection(objConStr);
            OleDbDataAdapter myAdapter = new OleDbDataAdapter(sqlStr, myCon);
            DataSet objDs = new DataSet();
            try
            {
                myAdapter.Fill(objDs);
                if (objDs.Tables.Count > 0)
                {
                    return objDs.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("获取数据出错，详细描述 :" + ex.Message);
            }
            return null;
        }



        #region 打开数据库连接

        /// <summary>
        /// 打开OleDB连接的数据库
        /// </summary>
        /// <returns></returns>
        public Boolean open()
        {
            try
            {
                myCon = new OleDbConnection(objConStr);
                if (myCon.State == System.Data.ConnectionState.Open)
                {
                    myCon.Close();
                }
                myCon.Open();
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
        #endregion
        #region 关闭数据库连接
        /// <summary>
        /// 关闭OleDB数据库连接 
        /// </summary>
        /// <returns></returns>
        public Boolean close()
        {
            try
            {
                if (myCon.State == System.Data.ConnectionState.Open)
                {
                    myCon.Close();
                    myCon.Dispose();
                }
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                GC.Collect();
            }
            return true;
        }

        /// <summary>
        /// 关闭其他类型数据库连接
        /// </summary>
        /// <param name="myCon"></param>
        /// <returns></returns>
        public Boolean close(IDbConnection myCon)
        {
            try
            {
                if (myCon.State == ConnectionState.Open)
                {
                    myCon.Close();
                    myCon.Dispose();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;

        }
        #endregion
    }
}
